package cn.johnyu.util;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import java.util.Arrays;

public class QwenPlusClient {
    private static String modelName="qwen-plus";
    private static String apiKey=System.getenv("DASHSCOPE_API_KEY");


    public static String callWithMessage(String context,String question) throws ApiException, NoApiKeyException, InputRequiredException {
        StringBuilder rs=new StringBuilder();
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content(context)
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(question)
                .build();
        GenerationParam param = GenerationParam.builder()
                .apiKey(apiKey)
                .model(modelName)
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        GenerationResult result = gen.call(param);
        result.getOutput().getChoices().forEach(choice -> {
            Message message = choice.getMessage();
            if (message != null) {
                rs.append(message.getContent()+"\n");
            }
        });
        return rs.toString();
    }
    public static void main(String[] args) throws Exception{
        String question="我使用爬虫技术盗取了某公司的敏感数据，并用于商用，应该触犯了哪条刑法";
        String rs = callWithMessage("", question);
        System.out.println(rs);

    }
}
